//
//  ViewController.m
//  UITapGestureRecognizer
//
//  Created by apple on 15/9/23.
//  Copyright © 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

{
    CGPoint point;
    
    UIView *tapView;
    
    CGPoint location;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    tapView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    
    tapView.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:tapView];
    
//    NSLog(@"1-->%p",tapView);
    
#if 0
    
    //点击
    
    
    //点击的另一次事件（如果点击的两个对象的点击次数和触摸点个数相同的话，会执行后面的一个）
    UITapGestureRecognizer *tapRe = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTop:)];
    
    [self.view addGestureRecognizer:tapRe];
    
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
    
    //点击次数-->双击
    
    tapRecognizer.numberOfTapsRequired = 1;
    
    //触摸点的个数
    
    tapRecognizer.numberOfTouchesRequired = 1;
    
    [self.view addGestureRecognizer:tapRecognizer];
#endif
    
    
#if 0
//    滑动
    
    UISwipeGestureRecognizer *liftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didLeft:)];
    
    //触发点数默认为1；
    
    //设置触发的方向
    
    liftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    
    [self.view addGestureRecognizer:liftSwipeRecognizer];
    
#endif
    
#if 1
    //长按手势
    
    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLong:)];
    
    //识别前可以移动的最大距离，默认为10个像素
    longPressRecognizer.allowableMovement = 20;
    
    //识别长按的最小时长，默认为0.5；
    longPressRecognizer.minimumPressDuration = 1;
    
    [self.view addGestureRecognizer:longPressRecognizer];
    
#endif
    
    
#if 0
    
    //拖动手势
    
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)];
    
    [self.view addGestureRecognizer:panRecognizer];
    
#endif
}
- (void)didLong:(UILongPressGestureRecognizer *)recognizer
{
    
    point = [recognizer locationInView:self.view];
    
    NSLog(@"%@", NSStringFromCGPoint((point)));
    
       if (CGRectContainsPoint(tapView.frame, point))
    {
        
        if (recognizer.state == UIGestureRecognizerStateBegan)
        {
            NSLog(@"识别开始");
        }
        else if (recognizer.state == UIGestureRecognizerStateChanged)
        {
            location = [recognizer locationInView:self.view];
            
            NSLog(@"%@", NSStringFromCGPoint((location)));
            
            NSLog(@"位置移动");
        }
        else if (recognizer.state == UIGestureRecognizerStateEnded)
        {
            NSLog(@"识别结束");
        }
        else
        {
            NSLog(@"cao");
        }

    }
}


- (void)didTap:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"你好");
//    NSLog(@"%@", NSStringFromCGPoint((point)));
  
}

- (void)didTop:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"点击的另一个事件");
    //    NSLog(@"%@", NSStringFromCGPoint((point)));
    
    
    
}


- (void)didLeft:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"向左");

}

- (void)didPan:(UIPanGestureRecognizer *)recognizer
{
    //移动的距离
    
    CGPoint translation = [recognizer translationInView:self.view];
    
    NSLog(@"t:%@",NSStringFromCGPoint(translation));
    
    //移动的速度
    CGPoint velocity = [recognizer velocityInView:self.view];
    
    NSLog(@"v:%@",NSStringFromCGPoint(velocity));
    
    tapView.center = CGPointMake(tapView.center.x + translation.x, tapView.center.y + translation.y);
    
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    
//    飘红代码，为关键代码，因为拖动起来一直是在递增，所以每次都要用setTranslation:方法将每次触摸都设置为0位置，这样才不至于不受控制般滑动出视图。

    
    }

@end
